Completed
Push — master ( 2f8415...e6e0b0 )
by Rain
02:22
created

ChangePassword.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
2
import _ from '_';
3
import ko from 'ko';
4
5
import {StorageResultType, Notification} from 'Common/Enums';
6
import {getNotificationFromResponse, i18n} from 'Common/Translator';
7
8
import Remote from 'Remote/User/Ajax';
9
10
import {getApp} from 'Helper/Apps/User';
11
import {command} from 'Knoin/Knoin';
12
13
class ChangePasswordUserSettings
14
{
15
	constructor() {
16
		this.changeProcess = ko.observable(false);
17
18
		this.errorDescription = ko.observable('');
19
		this.passwordMismatch = ko.observable(false);
20
		this.passwordUpdateError = ko.observable(false);
21
		this.passwordUpdateSuccess = ko.observable(false);
22
23
		this.currentPassword = ko.observable('');
24
		this.currentPassword.error = ko.observable(false);
25
		this.newPassword = ko.observable('');
26
		this.newPassword2 = ko.observable('');
27
28
		this.currentPassword.subscribe(() => {
29
			this.passwordUpdateError(false);
30
			this.passwordUpdateSuccess(false);
31
			this.currentPassword.error(false);
32
		});
33
34
		this.newPassword.subscribe(() => {
35
			this.passwordUpdateError(false);
36
			this.passwordUpdateSuccess(false);
37
			this.passwordMismatch(false);
38
		});
39
40
		this.newPassword2.subscribe(() => {
41
			this.passwordUpdateError(false);
42
			this.passwordUpdateSuccess(false);
43
			this.passwordMismatch(false);
44
		});
45
46
		this.onChangePasswordResponse = _.bind(this.onChangePasswordResponse, this);
47
	}
48
49
	@command((self) => !self.changeProcess() && '' !== self.currentPassword() && '' !== self.newPassword() && '' !== self.newPassword2())
50
	saveNewPasswordCommand() {
51
		if (this.newPassword() !== this.newPassword2())
52
		{
53
			this.passwordMismatch(true);
54
			this.errorDescription(i18n('SETTINGS_CHANGE_PASSWORD/ERROR_PASSWORD_MISMATCH'));
55
		}
56
		else
57
		{
58
			this.changeProcess(true);
59
60
			this.passwordUpdateError(false);
61
			this.passwordUpdateSuccess(false);
62
			this.currentPassword.error(false);
63
			this.passwordMismatch(false);
64
			this.errorDescription('');
65
66
			Remote.changePassword(this.onChangePasswordResponse, this.currentPassword(), this.newPassword());
67
		}
68
	}
69
70
	onHide() {
71
		this.changeProcess(false);
72
		this.currentPassword('');
73
		this.newPassword('');
74
		this.newPassword2('');
75
		this.errorDescription('');
76
		this.passwordMismatch(false);
77
		this.currentPassword.error(false);
78
	}
79
80
	onChangePasswordResponse(result, data) {
81
		this.changeProcess(false);
82
		this.passwordMismatch(false);
83
		this.errorDescription('');
84
		this.currentPassword.error(false);
85
86
		if (StorageResultType.Success === result && data && data.Result)
87
		{
88
			this.currentPassword('');
89
			this.newPassword('');
90
			this.newPassword2('');
91
92
			this.passwordUpdateSuccess(true);
93
			this.currentPassword.error(false);
94
95
			getApp().setClientSideToken(data.Result);
96
		}
97
		else
98
		{
99
			if (data && Notification.CurrentPasswordIncorrect === data.ErrorCode)
100
			{
101
				this.currentPassword.error(true);
102
			}
103
104
			this.passwordUpdateError(true);
105
			this.errorDescription(getNotificationFromResponse(data, Notification.CouldNotSaveNewPassword));
106
		}
107
	}
108
}
109
110
export {ChangePasswordUserSettings, ChangePasswordUserSettings as default};
111